<template>
  <el-dialog :title=" form.id? '编辑房产': '添加房产'" :visible="isShowDiaLog" @close="closeFn">

    <el-form :model="form" label-width="80px">
      <el-form-item label="经纪">
        <el-select v-model="agent">
          <el-option
            v-for="item in ecoData"
            :key="item.id"
            :label="item.name"
            :value="item.id"
          />

        </el-select>
      </el-form-item>
      <el-form-item label="分类">
        <el-select v-model="category">
          <el-option
            v-for="item in categoryData"
            :key="item.id"
            :label="item.title"
            :value="item.id"
          />

        </el-select>
      </el-form-item>
      <el-form-item label="标题">
        <el-input v-model="form.title" />
      </el-form-item>
      <el-form-item label="标签">
        <el-tag
          v-for="tag in dynamicTags"
          :key="tag"
          closable
          :disable-transitions="false"
          @close="handleClose(tag)"
        >
          {{ tag }}
        </el-tag>
        <el-input
          v-if="inputVisible"
          ref="saveTagInput"
          v-model="inputValue"
          class="input-new-tag"
          size="small"
          @keyup.enter.native="handleInputConfirm"
          @blur="handleInputConfirm"
        />
        <el-button v-else class="button-new-tag" size="small" @click="showInput">+ New Tag</el-button>
      </el-form-item>
      <el-form-item label="地址">
        <VDistpicker @selected="regionSelect" />
        <el-input v-model="form.address" />
      </el-form-item>
      <el-form-item label="价格">
        <el-input v-model="form.price" />
      </el-form-item>
      <el-form-item label="描述">
        <el-input v-model="form.desc" type="textarea" />
      </el-form-item>
      <el-form-item label="内容">
        <el-input v-model="form.content" />
      </el-form-item>
      <el-form-item label="介绍图片">
        <imgUpload ref="introduceImg" />
      </el-form-item>
      <el-form-item label="封面">
        <imgUpload ref="cover" :show="true" />
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button @click="closeFn">取 消</el-button>
      <el-button type="primary" @click="clickOk">确 定</el-button>
    </div>

  </el-dialog>

</template>

<script>
import { getEconomicList } from '@/api/economic'
import { getHouseById, getNewHouse, editHouse } from '@/api/house'
import imgUpload from './imageUpload.vue'
import VDistpicker from 'v-distpicker'
export default {
  components: {
    VDistpicker,
    imgUpload
  },
  props: {
    isShowDiaLog: {
      type: Boolean,
      default: false
    },
    // 分类数据
    categoryData: {
      type: Array,
      required: true
    },
    // 经纪数据
    ecoData: {
      type: Array,
      required: true
    }
  },
  data() {
    return {
      show: 'true',
      dialogImageUrl: '',
      dialogVisible: false,
      dynamicTags: [],
      inputVisible: false,
      inputValue: '',
      form: {
        id: '',
        title: '',
        desc: '',
        photos: [],
        image: '',
        agent: '',
        category: '',
        content: '',
        address: '',
        price: ''
      },
      agent: '',
      category: ''
    }
  },
  mounted() {
    this.getJngjiLists()
  },
  methods: {
    // 获取经济列表
    async getJngjiLists() {
      const res = await getEconomicList()
      this.form.agent = res
    },
    handleClose(tag) {
      this.dynamicTags.splice(this.dynamicTags.indexOf(tag), 1)
    },

    showInput() {
      this.inputVisible = true
      this.$nextTick(_ => {
        this.$refs.saveTagInput.$refs.input.focus()
      })
    },

    handleInputConfirm() {
      const inputValue = this.inputValue
      if (inputValue) {
        this.dynamicTags.push(inputValue)
      }
      this.inputVisible = false
      this.inputValue = ''
    },
    closeFn() {
      this.form = {
        id: '',
        title: '',
        desc: '',
        photos: [],
        image: '',
        agent: '',
        category: '',
        content: '',
        address: '',
        price: ''
      }
      this.category = ''
      this.agent = ''
      this.dynamicTags = []
      this.$refs.cover.fileList = []
      this.$refs.introduceImg.fileList = []
      this.$emit('update:isShowDiaLog', false)
    },
    regionSelect(data) {
      const { area, city, province } = data
      this.form.address = province.value + city.value + area.value
    },
    handlePictureCardPreview(file) {
      this.dialogImageUrl = file.url
      this.dialogVisible = true
    },
    async clickOk() {
      const fileList1 = this.$refs.introduceImg.fileList
      const fileList2 = this.$refs.cover.fileList

      this.form.image = fileList2[0].url
      if (this.form.id) {
        this.form.photos = fileList1.map(v => {
          return v.url
        })

        await editHouse({
          ...this.form,
          tags: this.dynamicTags,
          agent: this.agent,
          category: this.category
        })
        this.$message.success('编辑成功')
      } else {
        fileList1.forEach(v => {
          this.form.photos.push(v.url)
        })
        await getNewHouse({
          ...this.form,
          tags: this.dynamicTags

        })
        this.$message.success('添加成功')
      }

      // const fileList1 = this.$refs.introduceImg.fileList
      // // console.log('photos', fileList1)
      // fileList1.forEach(v => {
      //   this.form.photos.push(v.url)
      // })

      // const fileList2 = this.$refs.cover.fileList

      // this.form.image = fileList2[0].url

      this.$emit('addHouseList')

      this.$emit('update:isShowDiaLog', false)
    },
    async getHouseByIdFn(v) {
      this.form.id = v
      const res = await getHouseById(v)

      this.form = res
      console.log(this.form)
      if (this.form.agent) {
        this.agent = this.form.agent.id
      }
      if (this.form.category) {
        this.category = this.form.category.id
      }
      this.dynamicTags = this.form.tags

      this.$refs.introduceImg.fileList = this.form.photos.map(item => {
        return { url: item }
      })
      this.$refs.cover.fileList = [{ url: this.form.image }]
    }

  }

}
</script>

<style lang="scss" scoped>
.el-tag + .el-tag {
    margin-left: 10px;
  }
  .button-new-tag {
    margin-left: 10px;
    height: 32px;
    line-height: 30px;
    padding-top: 0;
    padding-bottom: 0;
  }
  .input-new-tag {
    width: 90px;
    margin-left: 10px;
    vertical-align: bottom;
  }
</style>
